package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.User;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private HttpSession session;

    @Autowired
    private RouteMapperExt routeMapperExt;

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private FavoriteMapperExt favoriteMapperExt;

    @Autowired
    private AffixService affixService;

    /**
     * 添加收藏
     *
     * @param favoriteVo
     * @return
     */
    @Override
    public Integer addFavorite(FavoriteVo favoriteVo) {
        //1、获得当前用户
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        if (EmptyUtil.isNullOrEmpty(userVo)) {
            //当前没有用户登录不能收藏
            return null;
        }
        //vo转pojo
        Favorite favorite = BeanConv.toBean(favoriteVo, Favorite.class);
        favorite.setUserId(userVo.getId());
        //添加收藏
        int i = favoriteMapper.insert(favorite);
        //修改收藏总条数
        Long j = routeMapperExt.updateRouteCountById(favorite.getRouteId());
        Route route = null;
        if (i > 0 && j > 0) {
            //查询最新收藏次数
            route = routeMapper.selectByPrimaryKey(favorite.getRouteId());
        }
        return route.getAttentionCount();
    }

    /**
     * 分页查询我的收藏
     *
     * @param pageNum  : 当前页页码
     * @param pageSize : 每页显示条数
     * @return PageInfo : 封装有本次分页查询的所有数据信息
     * pagehelper插件: Mybatis插件
     * 实现步骤:
     * 1.将当前页和每页显示条数这两个值绑定到当前线程上
     * 2.执行普通查询即可
     * 3.接收查询结果(这个结果pagehelper已经帮我们封装好了)
     * 实现原理:
     * Mybatis在执行查询时调用插件,会判断当前线程上是否有当前页和每页显示条数这两个值
     * 如果有这两个,Mybatis会帮我们实现分页查询
     * 插件使用步骤:
     * core模块中进行
     * 1.导入pagehelper插件包
     * 2.创建pagehelper插件对象
     * 3.配置pagehelper插件给Mybatis调用
     */
    @Override
    public PageInfo<RouteVo> findMyFavorite(int pageNum, int pageSize) {
        //获取当前登录用户
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        if (EmptyUtil.isNullOrEmpty(userVo)) {
            //当前没有用户登录,按钮可以点击
            return null;
        }
        // 1.将当前页和每页显示条数这两个值绑定到当前线程上
        PageHelper.startPage(pageNum, pageSize);
        // 2.执行普通查询即可
        List<Route> routeList = favoriteMapperExt.findFavoriteByUserId(userVo.getId());
        // 3.接收查询结果(这个结果pagehelper已经帮我们封装好了)
        // 查询完数据后,立即将查询结果封装到pageInfo中
        PageInfo<Route> routePageInfo = new PageInfo<>(routeList);
        // 4.对象转换
        PageInfo<RouteVo> routeVoPageInfo = new PageInfo<>();
        // 深度拷贝:拷贝本类及其父类中的所有数据
        // 浅度拷贝:只拷贝本类中的数据信息
        BeanConv.toBean(routePageInfo, routeVoPageInfo);
        // 转换查询结果对象
        List<RouteVo> routeVoList = BeanConv.toBeanList(routePageInfo.getList(), RouteVo.class);
        for (RouteVo routeVo : routeVoList) {
            // 查询线路包含的图片信息
            AffixVo affixVo = new AffixVo();
            affixVo.setBusinessId(routeVo.getId());
            List<AffixVo> affixVoList = affixService.findAffixByBusinessId(affixVo);
            // 将线路包含的图片集合封装到线路对象中
            routeVo.setAffixVoList(affixVoList);
        }
        routeVoPageInfo.setList(routeVoList);
        return routeVoPageInfo;
    }

    /**
     * 判断线路 是否收藏
     */
    @Override
    public Boolean isFavorited(FavoriteVo favoriteVo) {
        //获取当前登录用户
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        if (EmptyUtil.isNullOrEmpty(userVo)) {
            //当前没有用户登录,按钮可以点击
            return false;
        }
        //组装查询条件
        FavoriteExample example = new FavoriteExample();
        example.createCriteria()
                .andUserIdEqualTo(userVo.getId())
                .andRouteIdEqualTo(favoriteVo.getRouteId());
        List<Favorite> list = favoriteMapper.selectByExample(example);

        return !EmptyUtil.isNullOrEmpty(list);
    }

}
